#import <objc/runtime.h>
#import "MRApplication.h"

@interface MRApplication () <UIGestureRecognizerDelegate>

@property (nonatomic, weak) UIView		*input;

@property (nonatomic) CGFloat			keyboardOffset;
@property (nonatomic) UITapGestureRecognizer	*tapGestureRecognizer;

@end

@implementation MRApplication

- (void)setDelegate:(id<UIApplicationDelegate>)delegate
{
	[super setDelegate:delegate];
	
	_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:nil action:nil];
	_tapGestureRecognizer.delegate = self;
	_tapGestureRecognizer.enabled = NO;
	_tapGestureRecognizer.numberOfTouchesRequired = 1;
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)dealloc
{
	[_tapGestureRecognizer.view removeGestureRecognizer:_tapGestureRecognizer];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 编辑时位置
/*===================================================================
 说明:
	开始检测编辑，并调整至可见位置
 ==================================================================*/
- (void)keyboardWillShow:(NSNotification *)notification
{
	_input = nil;
	
	[self.keyWindow viewWithClass:[UIView class] handler:^(UIView *view, BOOL *stop) {
		if (view.isFirstResponder)
		{
			_input = view;
			*stop = YES;
		}
	}];
	
	//设置键盘背景色
	UIView *view = _input.inputView.superview;
	if ([MRSkinManager skin].nightMode && [NSStringFromClass([view class]) isEqualToString:@"UIInputSetHostView"])
	{
		view = view.subviews.firstObject;
		view.hidden = YES;
	}
	
	if (![_input isKindOfClass:[UITextView class]] && ![_input isKindOfClass:[UITextField class]]) return;
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
	
	//CGRect kb = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
	//CGRect rect = [_input convertRect:_input.bounds toView:_input.viewController.view];
	
	/*if ([_input isKindOfClass:[UITextField class]] && ((UITextField *)_input).borderStyle)
	{
		rect.size.height += 4;
	}*/
	
	/*if (CGRectGetMaxY(rect) > kb.origin.y)
	{
		_keyboardOffset = kb.origin.y - CGRectGetMaxY(rect);
		[self adjustWithOffset:_keyboardOffset];
	}*/

	//点击手势
	_tapGestureRecognizer.enabled = YES;
	[_tapGestureRecognizer.view removeGestureRecognizer:_tapGestureRecognizer];
	[self.keyWindow.subviews.firstObject addGestureRecognizer:_tapGestureRecognizer];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
	//[self adjustWithOffset:-_keyboardOffset];
	
	_keyboardOffset = 0;
	_tapGestureRecognizer.enabled = NO;
	[_tapGestureRecognizer.view removeGestureRecognizer:_tapGestureRecognizer];
}

/*===================================================================
 说明:
	如果点击不在本区域内，则取消当前焦点
 ==================================================================*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
	if (gestureRecognizer == _tapGestureRecognizer)
	{
		CGPoint point = [_tapGestureRecognizer locationInView:_input];
		if (!CGRectContainsPoint(_input.bounds, point))
		{
			[_input resignFirstResponder];
		}
		
		return NO;
	}
	
	return YES;
}

/*===================================================================
 说明:
	调整控制器视图位置
 ==================================================================*/
- (void)adjustWithOffset:(CGFloat)offset
{
	[UIView animateWithDuration:.25 animations:^{
		
		UIViewController *vc = _input.viewController;
		CGRect frame = vc.view.frame;
		
		frame.origin.y += offset;
		vc.view.frame = frame;
	}];
}

@end
